//
//  ViewController.swift
//  5.6.Assigning Characteristics to Your Dynamic Effects
//
//  Created by joker on 15/8/8.
//  Copyright (c) 2015年 joker. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var animator: UIDynamicAnimator?
    
    func newViewWithCenter(center: CGPoint, backgroundColor: UIColor) -> UIView {
        let newView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        newView.backgroundColor = backgroundColor
        newView.center = center
        return newView
    }
    
    override func viewDidAppear(animated: Bool) {
        let topView = newViewWithCenter(CGPoint(x: 100, y: 0), backgroundColor: UIColor.greenColor())
        let bottomView = newViewWithCenter(CGPoint(x: 100, y: 50), backgroundColor: UIColor.redColor())
        
        view.addSubview(topView)
        view.addSubview(bottomView)
        
        
        animator = UIDynamicAnimator(referenceView: view)
        
        let gravity = UIGravityBehavior(items: [topView,bottomView])
        animator!.addBehavior(gravity)
        
        let collision = UICollisionBehavior(items: [topView,bottomView])
        collision.translatesReferenceBoundsIntoBoundary = true
        
        animator!.addBehavior(collision)
        
        let moreElasticItem = UIDynamicItemBehavior(items: [bottomView])
        moreElasticItem.elasticity = 1
        
        let lessElasticItem = UIDynamicItemBehavior(items: [topView])
        lessElasticItem.elasticity = 0.5
        
        animator!.addBehavior(moreElasticItem)
        animator!.addBehavior(lessElasticItem)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

